/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import biz.ResultBO;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class PanelFitnessApprove extends javax.swing.JPanel {

    public static int ADMIN_MODE = 0;
    public static int COACH_MODE = 1;
    public static int MEMBER_MODE = 2;
    private MTableModel model;
    int mode;

    /**
     * Creates new form PanelFitnessApprove
     */
    public PanelFitnessApprove(int mode) {
        initComponents();
        this.mode = mode;
        try {
            switch (mode) {
                case 0: {
                    model = new MTableModel(new ResultBO().getAllUnapprovedResult(), MTableModel.RESULT_APP_MODE);
                }

                break;
                case 1: {
                    model = new MTableModel(new ResultBO().getAllResult(), MTableModel.RESULT_EDIT_MODE);
                    btnApprove.setVisible(false);

                }
                break;
                case 2: {
                    model = new MTableModel(new ResultBO().getAllResult(), MTableModel.RESULT_EDIT_MODE);
                    btnApprove.setVisible(false);
                }
            }
            tblDisplay.setModel(model);
        } catch (SQLException ex) {
            Logger.getLogger(PanelFitnessApprove.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public PanelFitnessApprove(int mode, int memberId) {
        if (mode == MEMBER_MODE) {
        initComponents();
            this.mode = mode;
            try {
                model = new MTableModel(new ResultBO().getResultById(memberId), MTableModel.MEMBER_MODE);
                btnApprove.setVisible(false);
                tblDisplay.setModel(model);
            } catch (SQLException ex) {
                Logger.getLogger(PanelFitnessApprove.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else new PanelFitnessApprove(mode);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        scrlDisplay = new javax.swing.JScrollPane();
        tblDisplay = new javax.swing.JTable();
        pnlControlBtn = new javax.swing.JPanel();
        btnApprove = new javax.swing.JButton();

        setBackground(new java.awt.Color(185, 192, 14));

        jPanel1.setBackground(new java.awt.Color(185, 192, 14));

        tblDisplay.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        scrlDisplay.setViewportView(tblDisplay);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(scrlDisplay, javax.swing.GroupLayout.DEFAULT_SIZE, 786, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(scrlDisplay, javax.swing.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );

        pnlControlBtn.setBackground(new java.awt.Color(225, 156, 24));

        btnApprove.setBackground(new java.awt.Color(0, 0, 0));
        btnApprove.setForeground(new java.awt.Color(255, 255, 255));
        btnApprove.setText("Approve");
        btnApprove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnApproveActionPerformed(evt);
            }
        });
        pnlControlBtn.add(btnApprove);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlControlBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlControlBtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnApproveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnApproveActionPerformed
        try {
            // TODO add your handling code here:
            int i = JOptionPane.showConfirmDialog(this, "Do you want to Approve this?", "Comfirm Dialog ", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (i == 0) // neu nguoi dung chon "Yes"
            {
                int index = (Integer) model.getValueAt(tblDisplay.getSelectedRow(), 0);
                new ResultBO().approveResult(index);
                model.removeRow(tblDisplay.getSelectedRow());
                model.fireTableDataChanged();
                JOptionPane.showMessageDialog(this, "Approve successful", "Message Dialog Demo", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Error: Database Link Error", "Message Dialog", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnApproveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnApprove;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel pnlControlBtn;
    private javax.swing.JScrollPane scrlDisplay;
    private javax.swing.JTable tblDisplay;
    // End of variables declaration//GEN-END:variables
}
